package me.arraylist.Test;

import me.arraylist.demo.Student.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentTest {
    public static void startStudentTest() {
        ArrayList<me.arraylist.demo.Student.Student> list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("-----欢迎来到学生管理系统-----");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");
            System.out.println("请输入您的选择");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1" -> addStudent(list);
                case "2" -> deleteStudent(list);
                case "3" -> updateStudent(list);
                case "4" -> queryStudent(list);
                case "5" -> {
                    System.out.println("退出");
                    break loop;
                }
                default -> System.out.println("您的输入有误");
            }
        }
    }

    public static void addStudent(ArrayList<me.arraylist.demo.Student.Student> list) {
        me.arraylist.demo.Student.Student stu = new me.arraylist.demo.Student.Student();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入学生id");
            String id = sc.next();
            boolean flag = contains(list, id);
            if (flag) {
                System.out.println("该id已存在,请重新录入");
            } else {
                stu.setId(id);
                break;
            }
        }
        System.out.println("请输入学生姓名");
        String name = sc.next();
        stu.setName(name);
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        stu.setAge(age);
        System.out.println("请输入学生地址");
        String address = sc.next();
        stu.setAddress(address);
        list.add(stu);
        System.out.println("添加成功");
    }

    public static void deleteStudent(ArrayList<me.arraylist.demo.Student.Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学生id");
        String id = sc.next();
        int index = getIndex(list, id);
        if (index >= 0) {
            list.remove(index);
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }

    }

    public static void updateStudent(ArrayList<me.arraylist.demo.Student.Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的id");
        String id = sc.next();
        int index = getIndex(list, id);
        if (index == -1) {
            System.out.println("该id不存在");
            return;
        }
        me.arraylist.demo.Student.Student stu = list.get(index);
        System.out.println("请输入新的姓名");
        String name = sc.next();
        stu.setName(name);
        System.out.println("请输入新的年龄");
        int age = sc.nextInt();
        stu.setAge(age);
        System.out.println("请输入新的地址");
        String address = sc.next();
        stu.setAddress(address);
        System.out.println("修改成功");
        list.set(index, stu);
    }

    public static void queryStudent(ArrayList<me.arraylist.demo.Student.Student> list) {
        if (list.size() == 0) {
            System.out.println("暂无学生信息");
            return;
        }
        System.out.println("id\tname\tage\taddress");
        for (int i = 0; i < list.size(); i++) {
            me.arraylist.demo.Student.Student stu = list.get(i);
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
        }
    }

    public static boolean contains(ArrayList<me.arraylist.demo.Student.Student> list, String id) {
        // for (int i = 0; i < list.size(); i++) {
        //  Student stu = list.get(i);
        //  if (stu.getId().equals(id)) {
        //      return true;
        //   }
        //     }
        return getIndex(list, id) >= 0;
    }

    public static int getIndex(ArrayList<me.arraylist.demo.Student.Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String sid = stu.getId();
            if (sid.equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
